package com.manli.api.service;

import com.manli.api.pojo.QueryParamsNoActivityGoods;
import com.manli.api.pojo.QueryParamsRecommendGoods;
import com.manli.api.util.ObjectEmptyUtil;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.RecommendGoodsDao;
import com.manli.api.dto.RecommendGoods;


@Service("recommendGoodsService")
public class RecommendGoodsService {
    @Autowired
    RecommendGoodsDao recommendGoodsDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParamsRecommendGoods params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("SELECT " +
                " a.*, " +
                " b.goods_name, " +
                " c.branch_name, " +
                " d.partner_name  " +
                "FROM " +
                " recommend_goods a " +
                " LEFT JOIN goods_def b ON a.goods_def_id = b.goods_def_id " +
                " LEFT JOIN branch c ON b.branch_id = c.branch_id " +
                " LEFT JOIN partner d ON b.partner_id = d.partner_id where 1=1 ");
        sql.append(params.getStatus() == null?"":" and a.status = '" + params.getStatus() + "'");
        sql.append(params.getActivityTypeId() == null?"":" and a.activity_type_id = " + params.getActivityTypeId());
        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<RecommendGoods> pageQuery = new PageQuery<RecommendGoods>(params.getPage(),params.getLimit());
        PageQuery<RecommendGoods> query =sqlManager.execute(sqlReady,RecommendGoods.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<RecommendGoods> list = recommendGoodsDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer recommendGoodsId) {
        RecommendGoods recommendGoods = recommendGoodsDao.single(recommendGoodsId);
        return ResultUtil.success(recommendGoods);
    }

    @Transactional
    public Result save(RecommendGoods recommendGoods) {
        ObjectEmptyUtil.recommendGoodsIsEmpty(recommendGoods);
        List<RecommendGoods> recommendGoodsList = recommendGoodsDao.createQuery()
                .andEq("goods_def_id",recommendGoods.getGoodsDefId())
                .andEq("status","1")
                .andEq("activity_type_id",recommendGoods.getActivityTypeId()).select();

        if(recommendGoodsList.size() > 0){
            return ResultUtil.error(-1,"该商品推荐已存在");
        }

        recommendGoodsDao.insert(recommendGoods);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(RecommendGoods recommendGoods) {
        ObjectEmptyUtil.recommendGoodsIsEmpty(recommendGoods);
        List<RecommendGoods> recommendGoodsList = recommendGoodsDao.createQuery()
                .andEq("recommend_goods_id",recommendGoods.getRecommendGoodsId()).select();

        if(recommendGoodsList.size() > 0){
            RecommendGoods recommendGoods1 = recommendGoodsList.get(0);
            if(recommendGoods1.getActivityTypeId().intValue() != recommendGoods.getActivityTypeId().intValue()){
                return ResultUtil.error(-1,"商品推荐类型不能修改");
            }
            if(recommendGoods1.getGoodsDefId().intValue() != recommendGoods.getGoodsDefId().intValue()){
                return ResultUtil.error(-1,"商品类型不能修改");
            }
        }
        recommendGoodsDao.updateTemplateById(recommendGoods);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(RecommendGoods recommendGoods) {
        recommendGoodsDao.updateTemplateById(recommendGoods);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            RecommendGoods recommendGoods1 = new RecommendGoods();
            recommendGoods1.setRecommendGoodsId(id);
            recommendGoods1.setStatus("0");
            recommendGoodsDao.updateTemplateById(recommendGoods1);
        }
        return ResultUtil.success();
    }


    public Result getNoActivityGoods(QueryParamsNoActivityGoods params) {
        if(params.getBranchId() == null && params.getPartnerId() == null){
            return ResultUtil.error(-1,"主店分店必须一个选择");
        }
        String sql = "SELECT " +
                " *  " +
                "FROM " +
                " goods_def  " +
                "WHERE " +
                " status = 1  " +
                (params.getBranchId() == null?"":" AND branch_id = " + params.getBranchId()) +
                (params.getPartnerId() == null?" AND partner_id is null":" AND partner_id =  " + params.getPartnerId()) +
                " AND goods_def_id NOT IN ( " +
                " SELECT " +
                "  goods_def_id  " +
                " FROM " +
                "  activity_goods  " +
                "WHERE " +
                " status = 1)";
        SQLReady sqlReady = new SQLReady(sql);
        List<Map> list = sqlManager.execute(sqlReady,Map.class);
        return ResultUtil.success(list);
    }
}
